<template>
    <div class="app-container">
        <levelbar></levelbar>
        <el-form ref="form" :model="form" :rules="rules" class="app-content" label-width="150px" label-position="right">
            <div class="box" v-loading="loading">
                <div class="appTitle">编辑-{{form.name}}</div>
                <div class="sales-title">
                    <span class="tip"></span>
                    <span class="title">基础信息</span>
                </div>
                <div class='chart-trend'>
                    <el-form-item label="收费项：">
                        <span>{{form.name}}</span>
                    </el-form-item>
                    <el-form-item label="备注：" prop="sRemark">
                        <el-input type="textarea" :maxlength="200" :rows="4" resize="none" v-model="form.sRemark" style="width:30%"></el-input>
                    </el-form-item>
                </div>
                <div class="sales-title">
                    <span class="tip"></span>
                    <span class="title">标准底价/元</span>
                </div>
                <div class='chart-trend'>
                    <el-row :gutter="21">
                        <el-col :span="7">
                            <el-form-item label="月收" prop="sBasePrice[0].price" :rules="rules.price">
                                <el-input v-model="form.sBasePrice[0].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="半年收" prop="sBasePrice[1].price" :rules="rules.price">
                                <el-input v-model="form.sBasePrice[1].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="年收" prop="sBasePrice[2].price" :rules="rules.price">
                                <el-input v-model="form.sBasePrice[2].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div class="sales-title">
                    <span class="tip"></span>
                    <span class="title">市场指导价/元</span>
                </div>
                <div class='chart-trend'>
                    <el-row :gutter="21">
                        <el-col :span="7">
                            <el-form-item label="月收" prop="sMarketPrice[0].price" :rules="rules.price">
                                <el-input v-model="form.sMarketPrice[0].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="半年收" prop="sMarketPrice[1].price" :rules="rules.price">
                                <el-input v-model="form.sMarketPrice[1].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="年收" prop="sMarketPrice[2].price" :rules="rules.price">
                                <el-input v-model="form.sMarketPrice[2].price" style="width:100%" :maxlength="7"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div class="sales-title">
                    <span class="tip"></span>
                    <span class="title">B端采购优惠 <span class="b-text-last">( 注:折扣为10，B端采购价=标准底价；折扣不为10，B端采购价=标准底价*折扣。)</span></span>
                </div>
                <div class="sales-title">
                    <el-row :gutter="21">
                        <el-col :span="6">
                            <el-form-item label="折扣" prop="sDiscount" :rules="rules.agio">
                                <el-input v-model="form.sDiscount" style="width:100%"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="1">
                            <p class="text-last">折</p>
                        </el-col>
                    </el-row>
                </div>
                <div class="sales-title">
                    <span class="tip"></span>
                    <span class="title">编辑记录</span>
                </div>
                <div class='chart-trend'>
                    <el-table :data="tableData" style="width: 100%" v-loading ="tableLoading">
                        <el-table-column type="index" width="50" label="序号"></el-table-column>
                        <el-table-column prop="creatorName" label="操作人"></el-table-column>
                        <el-table-column prop="detail" label="操作内容"></el-table-column>
                        <el-table-column prop="createTime" label="操作时间"></el-table-column>
                    </el-table>
                    <div class="pagination-container" style="text-align:right">
                        <el-pagination
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="listQuery.total"
                            :current-page.sync="listQuery.page"
                            :page-sizes="config.pageSizes"
                            :page-size="config.pageSize"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                </div>
                <div class="footer">
                    <el-row type="flex" justify="center">
                        <el-col :span="2">
                            <el-button @click="cancel">返回</el-button>
                        </el-col>
                        <el-col :span="2">
                            <el-button @click="btn('form')" type="primary">提交</el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </el-form>
    </div>
</template>
<script>
import Levelbar from '../../layout/Levelbar';
import { mapState, mapActions, mapGetters } from 'vuex';
import {updateHardwarePrice, pageMembershipServiceLog} from 'api/newVipApi';
export default {
    name: 'editVip',
    components: { Levelbar },
    data() {
        var filter = (rule, value, callback) => {
            var pattern = /\s/;
            if (pattern.test(value)) {
                callback(new Error('不允许输入空格'));
            } else {
                callback();
            }
        };
        return {
            rules: {
                count: [
                    { required: true, message: '内容不能为空', trigger: 'blur' },
                    { pattern: /^[1-9]\d*$/, message: '格式不正确', trigger: 'blur' },
                    { validator: (rule, value, callback) => {
                        if (Number(value)<1 || Number(value)>=101) {
                            callback(new Error('请输入1~100的整数'));
                        } else {
                            callback();
                        }
                    }, trigger: 'blur' }
                ],
                price: [
                    { required: true, message: '内容不能为空', trigger: 'blur' },
                    { validator: (rule, value, callback) => {
                        if (Number(value)<1 || Number(value)>=10000000) {
                            callback(new Error('请输入1~9,999,999的数字'));
                        } else {
                            callback();
                        }
                    }, trigger: 'blur' },
                    // { pattern: /^[1-9]\d{0,2}\.\d{2}$/, message: '请输入正确的格式,如：输入xx.xx', trigger: 'blur' }
                    { pattern: /^[1-9]\d*$/, message: '格式不正确', trigger: 'blur' }
                ],
                agio: [
                    { required: true, message: '内容不能为空', trigger: 'blur' },
                    { pattern: /^[0-9]{1}(\.[1-9])?$/, message: '格式不正确', trigger: 'blur' }
                ],
                // sIsDiscount: [
                //     { required: true, message: '请选择折扣类型', trigger: 'change' }
                // ],
                agioNumber: [
                    // { required: true, message: '内容不能为空', trigger: 'blur' },
                    { pattern: /^[1-9]\d*$/, message: '格式不正确' },
                    { validator: (rule, value, callback) => {
                        if (value) {
                            if (Number(value)<1 || Number(value)>=100) {
                                callback(new Error('输入范围1~99'));
                            } else {
                                callback();
                            }
                        } else {
                            callback();
                        }
                        
                    }, trigger: 'blur' }
                ],
                sRemark: [
                    { required: false, message: '请填写完整的备注'},
                    {
                        min: 1,
                        max: 100,
                        message: '长度为 1 到 100 个汉字',
                        trigger: 'blur'
                    },
                    {
                        validator: filter,
                        trigger: 'blur'
                    }
                ]
            },
            loading: false,
            tableLoading: false,
            listQuery: {// 分页
                page: 1,
                total: 0
            },
            form: {
                name: this.$route.params.name,
                sRemark: '',
                sStudyCount: '',
                sBasePrice: [
                    {
                        value: '1',
                        name: '1个月',
                        price: ''
                    },
                    {
                        value: '6',
                        name: '6个月',
                        price: ''
                    },
                    {
                        value: '12',
                        name: '12个月',
                        price: ''
                    }
                ],
                sMarketPrice: [
                    {
                        value: '1',
                        name: '1个月',
                        price: ''
                    },
                    {
                        value: '6',
                        name: '6个月',
                        price: ''
                    },
                    {
                        value: '12',
                        name: '12个月',
                        price: ''
                    }
                ],
                sIsDiscount: '',
                options: [
                    {
                        value: '0',
                        label: '购买其他服务时优惠'
                    },
                    {
                        value: '1',
                        label: '自定义'
                    }
                ],
                sDiscount: ''
            },
            tableData: []
        };
    },
    computed: {
        ...mapState({
            config: state => state.config.table
        }),
        ...mapGetters([
            'uid'
        ])
    },
    created() {
        this.searchFetchData();
    },
    methods: {
        // 全局获取行数
        ...mapActions(['ChangePageSize']),
        // 分页
        handleSizeChange(val) {
            this.listQuery.page = 1;
            this.tableLoading = true;
            this.ChangePageSize(val);
            this.searchFetchData();
        },
        handleCurrentChange(val) {
            this.tableLoading = true;
            this.listQuery.page = val;
            this.searchFetchData();
        },
        cancel() {
            this.$router.go(-1);
        },
        searchFetchData() {
            this.tableLoading = true;
            pageMembershipServiceLog(this.$route.params.id, this.listQuery.page, this.config.pageSize).then(data => {
                this.tableData = data.data.content.list;
                this.listQuery.total = data.data.content.pagination.total;
                this.tableLoading = false;
            }).catch(() => {
                this.tableLoading = false;
            })
        },
        btn(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading= true;
                    updateHardwarePrice(
                        this.$route.params.id,
                        this.form.sRemark,
                        JSON.stringify(this.fifterZer(this.form.sBasePrice)),
                        JSON.stringify(this.fifterZer(this.form.sMarketPrice)),
                        this.form.sIsDiscount,
                        this.form.sDiscount
                    ).then(data => {
                        this.loading= false;
                        this.delZero(this.form.sBasePrice);
                        this.delZero(this.form.sMarketPrice);
                        this.$router.push({path: '/newVip'});
                    }).catch(() => {
                        this.loading= false;
                        this.delZero(this.form.sBasePrice);
                        this.delZero(this.form.sMarketPrice);
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        fifterZer(v) {
            v.forEach(e => {
                e.price += '.00'
            })
            return v;
        },
        delZero(v) {
            v.forEach(e => {
                if (e.price.indexOf('.00') != -1) {
                    e.price = e.price.split('.00')[0]
                }
            })
            return v;
        }
    }
};
</script>
<style lang="scss" scoped>
.mr{
    line-height: 35px;
}
.text-last{
    line-height: 35px;
    font-size: 14px;
    color: #666;
}
.b-text{
    color: #666;
    font-size: 14px;
    line-height: 1;
    padding: 11px 12px 11px 58px;
    box-sizing: border-box;
    margin-bottom: 30px;
}
.b-text-last{
    font-size:14px;
    font-family:MicrosoftYaHei;
    color:#FF5E2C;
    margin-left: 15px;
}
@mixin tip-css {
    display: inline-block;
    height: 14px;
    width: 4px;
    border-left: 3px solid #ff5e2c;
    padding-left: 10px;
    vertical-align: text-top;
}
.sales-title {
    margin-bottom: 40px;
    margin-top: 40px;
    .tip {
        @include tip-css();
    }
    .title {
        color: #333;
    }
}
.app-container {
    position: relative;
    padding: 20px 20px 10px;
    .app-levelbar {
        height: 35px;
    }
    .table{
        text-align:left;
        th,td{
            padding:0 20px;
            width:200px;
            height:40px;
            font-size:14px;
        }
        a{
            font-size:15px;
            color:#ff5e2c;
        }
    }
    .app-content{
        .box{
            padding:0 50px;
        }
        box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
        -webkit-box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
        background:#fff;
        padding-bottom:50px;
        .appTop{
            margin: 40px 0;
            font-size: 14px;
            color: #333;
            line-height: 14px;
            border-left: 4px solid #ff5e2c;
            text-indent: 16px;
        }
        .appTitle{
            padding-top:8px;
            text-align: center;
            font-size: 18px;
            color: #333;
            line-height: 72px;
            font-weight: bold;
            font-family: Microsoft YaHei;
            border-bottom: 1px solid #ddd;
        }
        
        .footer {
            text-align: center;
            margin-top: 60px;
        }
    }
    .dialog-footer{
        .el-button{
            margin: 80px 0 10px 0;
        }
        text-align: center;
    }
    .dialogTable{
        text-align:center;
        margin-left:50px;
        margin-top:30px;
        th{
            background:#f3f3f3;
        }
        th,td{
            text-align:center;
            padding:0 20px;
            border: solid 1px #ECECEC;
            width:220px;
            height:40px;
        }
    }
    .dialog-footer3{
        .el-button{
            margin: 50px 0 10px 0;
        }
        text-align: center;
    }
    .dialogTable3{
        text-align:center;
        margin-top:30px;
        margin:30px 50px;
        tr td:nth-of-type(1){
            width:30%;
        }
        tr td:nth-of-type(2){
            width:310px;
        }
        th,td{
            text-align:center;
            padding:0 20px;
            border: solid 1px #ECECEC;
            width:220px;
            height:40px;
        }
    }
    
}
</style>
